###R code for measurement model and SEM with bootstrap

library(dplyr)
library(lavaan)
library(semTools)


###subset countries
Corona_Culture_US <-subset(Corona_Culture,Country_ID=="1")
Corona_Culture_China <-subset(Corona_Culture,Country_ID=="0")

###measurement model
##Initial measurement model
##hands model
Measure.model1="
Egal1=~S2_CT_7+S2_CT_8+S2_CT_9 
Hier1=~S2_CT_1+S2_CT_2+S2_CT_3
Fatal1=~S2_CT_10+S2_CT_11+S2_CT_12
Indi1=~S2_CT_4+S2_CT_5+S2_CT_6
RiskPerc=~S2_Risk_Personal_NoAction+S2_Risk_Country+S2_Risk_Global+S2_Dread
Trust1=~S2_Trust_Central+S2_Trust_State+S2_Trust_Local+S2_Trust_CDC
ActionPerc1=~S2_Wash_Risks_low_household+S2_Wash_Risks_low_community+S2_Wash_Resources"

Measurefit1 <- lavaan::cfa(Measure.model1, 
                           data=data_new,sample.cov = NULL,sample.mean = NULL,group = "Country_ID")


fitMeasures(Measurefit1)

##mask model
Measure.model2="
Egal1=~S2_CT_7+S2_CT_8+S2_CT_9 
Hier1=~S2_CT_1+S2_CT_2+S2_CT_3
Fatal1=~S2_CT_10+S2_CT_11+S2_CT_12
Indi1=~S2_CT_4+S2_CT_5+S2_CT_6
RiskPerc=~S2_Risk_Personal_NoAction+S2_Risk_Country+S2_Risk_Global+S2_Dread
Trust1=~S2_Trust_Central+S2_Trust_State+S2_Trust_Local+S2_Trust_CDC
ActionPerc1=~S2_Mask_Risks_low_household+S2_Mask_Risks_low_community+S2_Mask_Resources"

Measurefit2 <- lavaan::cfa(Measure.model2, 
                           data=data_new,sample.cov = NULL,sample.mean = NULL,group = "Country_ID")
fitMeasures(Measurefit2)

##gathering model
Measure.model3="
Egal1=~S2_CT_7+S2_CT_8+S2_CT_9 
Hier1=~S2_CT_1+S2_CT_2+S2_CT_3
Fatal1=~S2_CT_10+S2_CT_11+S2_CT_12
Indi1=~S2_CT_4+S2_CT_5+S2_CT_6
RiskPerc=~S2_Risk_Personal_NoAction+S2_Risk_Country+S2_Risk_Global+S2_Dread
Trust1=~S2_Trust_Central+S2_Trust_State+S2_Trust_Local+S2_Trust_CDC
ActionPerc1=~S2_Avoid_public_Risks_low_household+S2_Avoid_public_Risks_low_community+S2_Avoid_public_Resources"

Measurefit3 <- lavaan::cfa(Measure.model3, 
                           data=data_new,sample.cov = NULL,sample.mean = NULL,group = "Country_ID")
fitMeasures(Measurefit3)

##vacci model
Measure.model4="
Egal1=~S2_CT_7+S2_CT_8+S2_CT_9 
Hier1=~S2_CT_1+S2_CT_2+S2_CT_3
Fatal1=~S2_CT_10+S2_CT_11+S2_CT_12
Indi1=~S2_CT_4+S2_CT_5+S2_CT_6
RiskPerc=~S2_Risk_Personal_NoAction+S2_Risk_Country+S2_Risk_Global+S2_Dread
Trust1=~S2_Trust_Central+S2_Trust_State+S2_Trust_Local+S2_Trust_CDC
ActionPerc1=~S2_Vaccinate_Risks_low_household+S2_Vaccinate_Risks_low_community+S2_Vacinate_Resources"

Measurefit4 <- lavaan::cfa(Measure.model4, 
                           data=data_new,sample.cov = NULL,sample.mean = NULL,group = "Country_ID")
fitMeasures(Measurefit4)


##Revised measurement model

##Hands measurement model
#Configural invariance

hands_measure_model<-'Egal1=~S2_CT_7+S2_CT_8+S2_CT_9
Hier1=~S2_CT_1+S2_CT_2+S2_CT_3
Fatal1=~S2_CT_10+S2_CT_11+S2_CT_12
Indi1=~S2_CT_4+S2_CT_5+S2_CT_6
RiskPerc=~S2_Risk_Personal_NoAction+S2_Risk_Country+S2_Dread
trust1=~S2_Trust_State+S2_Trust_Local+S2_Trust_CDC
ActionPerc1=~S2_Wash_Risks_low_household+S2_Wash_Risks_low_community+S2_Wash_Resources'

#Configural invariance
hands_measure_model_fit1 <- lavaan::cfa(hands_measure_model, data=data_new,
                                        sample.cov = NULL,meanstructure=TRUE,group = "Country_ID")
fitMeasures(hands_measure_model_fit1)



#Metric invariance
hands_measure_model_fit2<-lavaan::cfa(hands_measure_model, 
                                      data=data_new,
                                      group="Country_ID",
                                      meanstructure=TRUE,
                                      group.equal = "loadings")

fitMeasures(hands_measure_model_fit2)



#Scalar invariance
hands_measure_model_fit3<-lavaan::cfa(hands_measure_model, data=data_new,
                                      sample.cov = NULL,meanstructure=TRUE,group = "Country_ID",
                                      group.equal =c("intercepts", "loadings"))

fitMeasures(hands_measure_model_fit3)


##Mask measurement model
#Configural invariance

mask_measure_model<-'Egal1=~S2_CT_7+S2_CT_8+S2_CT_9
Hier1=~S2_CT_1+S2_CT_2+S2_CT_3
Fatal1=~S2_CT_10+S2_CT_11+S2_CT_12
Indi1=~S2_CT_4+S2_CT_5+S2_CT_6
RiskPerc=~S2_Risk_Personal_NoAction+S2_Risk_Country+S2_Dread
Trust1=~S2_Trust_State+S2_Trust_Local+S2_Trust_CDC
ActionPerc1=~S2_Mask_Risks_low_household+S2_Mask_Risks_low_community+S2_Mask_Resources'

#Configural invariance
mask_measure_model_fit1 <- lavaan::cfa(mask_measure_model, data=data_new,
                                       sample.cov = NULL,meanstructure=TRUE,group = "Country_ID")
fitMeasures(mask_measure_model_fit1)

#Metric invariance
mask_measure_model_fit2<-lavaan::cfa(mask_measure_model, 
                                     data=data_new,
                                     group="Country_ID",
                                     meanstructure=TRUE,
                                     group.equal = "loadings")

fitMeasures(mask_measure_model_fit2)

#Scalar invariance
mask_measure_model_fit3<-lavaan::cfa(mask_measure_model, data=data_new,
                                     sample.cov = NULL,meanstructure=TRUE,group = "Country_ID",
                                     group.equal =c("intercepts", "loadings"))

fitMeasures(mask_measure_model_fit3)


##Gathering measurement model
#Configural invariance

avoid_measure_model<-'Egal1=~S2_CT_7+S2_CT_8+S2_CT_9
Hier1=~S2_CT_1+S2_CT_2+S2_CT_3
Fatal1=~S2_CT_10+S2_CT_11+S2_CT_12
Indi1=~S2_CT_4+S2_CT_5+S2_CT_6
RiskPerc=~S2_Risk_Personal_NoAction+S2_Risk_Country+S2_Dread
Trust1=~S2_Trust_State+S2_Trust_Local+S2_Trust_CDC
ActionPerc1=~S2_Avoid_public_Risks_low_household+S2_Avoid_public_Risks_low_community+S2_Avoid_public_Resources'

#Configural invariance
avoid_measure_model_fit1 <- lavaan::cfa(avoid_measure_model, data=data_new,
                                        sample.cov = NULL,meanstructure=TRUE,group = "Country_ID")
fitMeasures(avoid_measure_model_fit1)

#Metric invariance
avoid_measure_model_fit2<-lavaan::cfa(avoid_measure_model, 
                                      data=data_new,
                                      group="Country_ID",
                                      meanstructure=TRUE,
                                      group.equal = "loadings")

fitMeasures(avoid_measure_model_fit2)

#Scalar invariance
avoid_measure_model_fit3<-lavaan::cfa(avoid_measure_model, data=data_new,
                                      sample.cov = NULL,meanstructure=TRUE,group = "Country_ID",
                                      group.equal =c("intercepts", "loadings"))

fitMeasures(avoid_measure_model_fit3)

##Vacci measurement model
#Configural invariance
vaccinate_measure_model<-'Egal1=~S2_CT_7+S2_CT_8+S2_CT_9
Hier1=~S2_CT_1+S2_CT_2+S2_CT_3
Fatal1=~S2_CT_10+S2_CT_11+S2_CT_12
Indi1=~S2_CT_4+S2_CT_5+S2_CT_6
RiskPerc=~S2_Risk_Personal_NoAction+S2_Risk_Country+S2_Dread
Trust1=~S2_Trust_State+S2_Trust_Local+S2_Trust_CDC
ActionPerc1=~S2_Vaccinate_Risks_low_household+S2_Vaccinate_Risks_low_community+S2_Vacinate_Resources'

#Configural invariance
vaccinate_measure_model_fit1 <- lavaan::cfa(vaccinate_measure_model, data=data_new,
                                            sample.cov = NULL,meanstructure=TRUE,group = "Country_ID")
fitMeasures(vaccinate_measure_model_fit1)

#Metric invariance
vaccinate_measure_model_fit2<-lavaan::cfa(vaccinate_measure_model, 
                                          data=data_new,
                                          group="Country_ID",
                                          meanstructure=TRUE,
                                          group.equal = "loadings")

fitMeasures(vaccinate_measure_model_fit2)

#Scalar invariance
vaccinate_measure_model_fit3<-lavaan::cfa(vaccinate_measure_model, data=data_new,
                                          sample.cov = NULL,meanstructure=TRUE,group = "Country_ID",
                                          group.equal =c("intercepts", "loadings"))

fitMeasures(vaccinate_measure_model_fit3)



###SEM with bootstrap


### Select columns of the dataframe

Corona_Culture<-select(data_new,Country_ID,S2_CT_7,S2_CT_8,S2_CT_9,
                       S2_CT_1,S2_CT_2,S2_CT_3
                       ,S2_CT_10,S2_CT_11,S2_CT_12
                       ,S2_CT_4,S2_CT_5,S2_CT_6
                       ,S2_Risk_Personal_NoAction,S2_Risk_Country,S2_Dread
                       ,S2_Trust_State,S2_Trust_CDC,S2_Trust_Local
                       ,S2_Wash_Risks_low_household,S2_Wash_Risks_low_community,S2_Wash_Resources,
                       S2_Mask_Risks_low_household,S2_Mask_Risks_low_community,S2_Mask_Resources,
                       S2_Avoid_public_Risks_low_household,S2_Avoid_public_Risks_low_community,S2_Avoid_public_Resources,
                       S2_Vaccinate_Risks_low_household,S2_Vaccinate_Risks_low_community,S2_Vacinate_Resources,
                       S2_Wash_Household_action,S2_Masks_Household_action,S2_Avoid_public_Household_action,S2_Vaccinate_Household_action)
#????missing value
Corona_Culture_nm<-na.omit(Corona_Culture)

##hands
hands_model<-'Egal1=~S2_CT_7+S2_CT_8+S2_CT_9
Hier1=~S2_CT_1+S2_CT_2+S2_CT_3
Fatal1=~S2_CT_10+S2_CT_11+S2_CT_12
Indi1=~S2_CT_4+S2_CT_5+S2_CT_6
RiskPerc=~S2_Risk_Personal_NoAction+S2_Risk_Country+S2_Dread
Trust=~S2_Trust_State+S2_Trust_Local+S2_Trust_CDC
ActionPerc1=~S2_Wash_Risks_low_household+S2_Wash_Risks_low_community+S2_Wash_Resources
RiskPerc~c(a1group1, a1group2)*Egal1+c(b1group1, b1group2)*Hier1+c(c1group1, c1group2)*Indi1+c(d1group1, d1group2)*Fatal1
Trust~c(a2group1, a2group2)*Egal1+c(b2group1, b2group2)*Hier1+c(c2group1, c2group2)*Indi1+c(d2group1, d2group2)*Fatal1
ActionPerc1~c(a3group1, a3group2)*Egal1+c(b3group1, b3group2)*Hier1+c(c3group1, c3group2)*Indi1+c(d3group1, d3group2)*Fatal1
S2_Wash_Household_action~c(a4group1, a4group2)*Egal1+c(b4group1, b4group2)*Hier1+c(c4group1, c4group2)*Indi1+c(d4group1, d4group2)*Fatal1+c(egroup1, egroup2)*RiskPerc+c(fgroup1, fgroup2)*Trust+c(ggroup1, ggroup2)*ActionPerc1
##indirect effect for egal
a1e_g1 := a1group1*egroup1 
a1e_g2 := a1group2*egroup2
a2f_g1:= a2group1*fgroup1 
a2f_g2:= a2group2*fgroup2
a3g_g1:= a3group1*ggroup1 
a3g_g2:= a3group2*ggroup2
ID_g1_E:=(a1group1*egroup1)+ (a2group1*fgroup1)+(a3group1*ggroup1 )
ID_g2_E:=(a1group2*egroup2)+ (a2group2*fgroup2)+(a3group2*ggroup2)
total1_group1 := a4group1 + (a1group1*egroup1)+ (a2group1*fgroup1)+(a3group1*ggroup1 )
total1_group2 := a4group2 + (a1group2*egroup2)+ (a2group2*fgroup2)+(a3group2*ggroup2)
##indirect effect for hier
b1e_g1 := b1group1*egroup1 
b1e_g2 := b1group2*egroup2
b2f_g1:= b2group1*fgroup1 
b2f_g2:= b2group2*fgroup2
b3g_g1:= b3group1*ggroup1 
b3g_g2:= b3group2*ggroup2
ID_g1_H:= (b1group1*egroup1)+ (b2group1*fgroup1)+(b3group1*ggroup1 )
ID_g2_H:= (b1group2*egroup2)+ (b2group2*fgroup2)+(b3group2*ggroup2)
total1_group1 := b4group1 + (b1group1*egroup1)+ (b2group1*fgroup1)+(b3group1*ggroup1 )
total1_group2 := b4group2 + (b1group2*egroup2)+ (b2group2*fgroup2)+(b3group2*ggroup2)
##indirect effect for indi
c1e_g1 := c1group1*egroup1 
c1e_g2 := c1group2*egroup2
c2f_g1:= c2group1*fgroup1 
c2f_g2:= c2group2*fgroup2
c3g_g1:= c3group1*ggroup1 
c3g_g2:= c3group2*ggroup2
ID_g1_I:= (c1group1*egroup1)+ (c2group1*fgroup1)+(c3group1*ggroup1 )
ID_g2_I:= (c1group2*egroup2)+ (c2group2*fgroup2)+(c3group2*ggroup2)
total1_group1 := c4group1 + (c1group1*egroup1)+ (c2group1*fgroup1)+(c3group1*ggroup1 )
total1_group2 := c4group2 + (c1group2*egroup2)+ (c2group2*fgroup2)+(c3group2*ggroup2)
##indirect effect for fatal
d1e_g1 := d1group1*egroup1 
d1e_g2 := d1group2*egroup2
d2f_g1:= d2group1*fgroup1 
d2f_g2:= d2group2*fgroup2
d3g_g1:= d3group1*ggroup1 
d3g_g2:= d3group2*ggroup2
ID_g1_F:= (d1group1*egroup1)+ (d2group1*fgroup1)+(d3group1*ggroup1 )
ID_g2_F:= (d1group2*egroup2)+ (d2group2*fgroup2)+(d3group2*ggroup2)
total1_group1 := d4group1 + (d1group1*egroup1)+ (d2group1*fgroup1)+(d3group1*ggroup1 )
total1_group2 := d4group2 + (d1group2*egroup2)+ (d2group2*fgroup2)+(d3group2*ggroup2)
'




hands_model_boot_fit<-lavaan::sem(hands_model, data=Corona_Culture,std.lv=TRUE,
                                  meanstructure=TRUE,group = "Country_ID",
                                  test="bootstrap",bootstrap=5000)
summary(hands_model_boot_fit,
        fit.measure=TRUE,
        standardized=TRUE,
        rsquare=TRUE,ci=TRUE)

options(max.print=1000000)

parameter_hands_boot<-parameterEstimates(hands_model_boot_fit, standardized=TRUE, se = TRUE, zstat = TRUE, pvalue = TRUE, ci = TRUE, 
                                         level = 0.95, boot.ci.type = "bca.simple")


##mask

mask_model<-'Egal1=~S2_CT_7+S2_CT_8+S2_CT_9
Fatal1=~S2_CT_10+S2_CT_11+S2_CT_12
Hier1=~S2_CT_1+S2_CT_2+S2_CT_3
Indi1=~S2_CT_4+S2_CT_5+S2_CT_6
RiskPerc=~S2_Risk_Personal_NoAction+S2_Risk_Country+S2_Dread
Trust=~S2_Trust_State+S2_Trust_Local+S2_Trust_CDC
ActionPerc1=~S2_Mask_Risks_low_household+S2_Mask_Risks_low_community+S2_Mask_Resources
RiskPerc~c(a1group1, a1group2)*Egal1+c(b1group1, b1group2)*Hier1+c(c1group1, c1group2)*Indi1+c(d1group1, d1group2)*Fatal1
Trust~c(a2group1, a2group2)*Egal1+c(b2group1, b2group2)*Hier1+c(c2group1, c2group2)*Indi1+c(d2group1, d2group2)*Fatal1
ActionPerc1~c(a3group1, a3group2)*Egal1+c(b3group1, b3group2)*Hier1+c(c3group1, c3group2)*Indi1+c(d3group1, d3group2)*Fatal1
S2_Masks_Household_action~c(a4group1, a4group2)*Egal1+c(b4group1, b4group2)*Hier1+c(c4group1, c4group2)*Indi1+c(d4group1, d4group2)*Fatal1+c(egroup1, egroup2)*RiskPerc+c(fgroup1, fgroup2)*Trust+c(ggroup1, ggroup2)*ActionPerc1
##indirect effect for egal
a1e_g1 := a1group1*egroup1 
a1e_g2 := a1group2*egroup2
a2f_g1:= a2group1*fgroup1 
a2f_g2:= a2group2*fgroup2
a3g_g1:= a3group1*ggroup1 
a3g_g2:= a3group2*ggroup2
ID_g1_E:=(a1group1*egroup1)+ (a2group1*fgroup1)+(a3group1*ggroup1 )
ID_g2_E:=(a1group2*egroup2)+ (a2group2*fgroup2)+(a3group2*ggroup2)
total1_group1 := a4group1 + (a1group1*egroup1)+ (a2group1*fgroup1)+(a3group1*ggroup1 )
total1_group2 := a4group2 + (a1group2*egroup2)+ (a2group2*fgroup2)+(a3group2*ggroup2)
##indirect effect for hier
b1e_g1 := b1group1*egroup1 
b1e_g2 := b1group2*egroup2
b2f_g1:= b2group1*fgroup1 
b2f_g2:= b2group2*fgroup2
b3g_g1:= b3group1*ggroup1 
b3g_g2:= b3group2*ggroup2
ID_g1_H:= (b1group1*egroup1)+ (b2group1*fgroup1)+(b3group1*ggroup1 )
ID_g2_H:= (b1group2*egroup2)+ (b2group2*fgroup2)+(b3group2*ggroup2)
total1_group1 := b4group1 + (b1group1*egroup1)+ (b2group1*fgroup1)+(b3group1*ggroup1 )
total1_group2 := b4group2 + (b1group2*egroup2)+ (b2group2*fgroup2)+(b3group2*ggroup2)
##indirect effect for indi
c1e_g1 := c1group1*egroup1 
c1e_g2 := c1group2*egroup2
c2f_g1:= c2group1*fgroup1 
c2f_g2:= c2group2*fgroup2
c3g_g1:= c3group1*ggroup1 
c3g_g2:= c3group2*ggroup2
ID_g1_I:= (c1group1*egroup1)+ (c2group1*fgroup1)+(c3group1*ggroup1 )
ID_g2_I:= (c1group2*egroup2)+ (c2group2*fgroup2)+(c3group2*ggroup2)
total1_group1 := c4group1 + (c1group1*egroup1)+ (c2group1*fgroup1)+(c3group1*ggroup1 )
total1_group2 := c4group2 + (c1group2*egroup2)+ (c2group2*fgroup2)+(c3group2*ggroup2)
##indirect effect for fatal
d1e_g1 := d1group1*egroup1 
d1e_g2 := d1group2*egroup2
d2f_g1:= d2group1*fgroup1 
d2f_g2:= d2group2*fgroup2
d3g_g1:= d3group1*ggroup1 
d3g_g2:= d3group2*ggroup2
ID_g1_F:= (d1group1*egroup1)+ (d2group1*fgroup1)+(d3group1*ggroup1 )
ID_g2_F:= (d1group2*egroup2)+ (d2group2*fgroup2)+(d3group2*ggroup2)
total1_group1 := d4group1 + (d1group1*egroup1)+ (d2group1*fgroup1)+(d3group1*ggroup1 )
total1_group2 := d4group2 + (d1group2*egroup2)+ (d2group2*fgroup2)+(d3group2*ggroup2)'


mask_model_boot_fit<-lavaan::sem(mask_model, data=Corona_Culture,std.lv=TRUE,
                                 meanstructure=TRUE,estimator = "ML",group = "Country_ID",
                                 group.equal = "loadings",test="bootstrap",bootstrap=5000)
summary(mask_model_boot_fit,
        fit.measure=TRUE,
        standardized=TRUE,
        rsquare=TRUE,ci=TRUE)

options(max.print=100000)
parameterEstimates(mask_model_boot_fit, standardized=TRUE, se = TRUE, zstat = TRUE, pvalue = TRUE, ci = TRUE, 
                   level = 0.95, boot.ci.type = "bca.simple")


##Avoid agtherings
avoid_model<-'Egal1=~S2_CT_7+S2_CT_8+S2_CT_9
Fatal1=~S2_CT_10+S2_CT_11+S2_CT_12
Hier1=~S2_CT_1+S2_CT_2+S2_CT_3
Indi1=~S2_CT_4+S2_CT_5+S2_CT_6
RiskPerc=~S2_Risk_Personal_NoAction+S2_Risk_Country+S2_Dread
Trust=~S2_Trust_State+S2_Trust_Local+S2_Trust_CDC
ActionPerc1=~S2_Avoid_public_Risks_low_household+S2_Avoid_public_Risks_low_community+S2_Avoid_public_Resources
RiskPerc~c(a1group1, a1group2)*Egal1+c(b1group1, b1group2)*Hier1+c(c1group1, c1group2)*Indi1+c(d1group1, d1group2)*Fatal1
Trust~c(a2group1, a2group2)*Egal1+c(b2group1, b2group2)*Hier1+c(c2group1, c2group2)*Indi1+c(d2group1, d2group2)*Fatal1
ActionPerc1~c(a3group1, a3group2)*Egal1+c(b3group1, b3group2)*Hier1+c(c3group1, c3group2)*Indi1+c(d3group1, d3group2)*Fatal1
S2_Avoid_public_Household_action~c(a4group1, a4group2)*Egal1+c(b4group1, b4group2)*Hier1+c(c4group1, c4group2)*Indi1+c(d4group1, d4group2)*Fatal1+c(egroup1, egroup2)*RiskPerc+c(fgroup1, fgroup2)*Trust+c(ggroup1, ggroup2)*ActionPerc1
##indirect effect for egal
a1e_g1 := a1group1*egroup1 
a1e_g2 := a1group2*egroup2
a2f_g1:= a2group1*fgroup1 
a2f_g2:= a2group2*fgroup2
a3g_g1:= a3group1*ggroup1 
a3g_g2:= a3group2*ggroup2
ID_g1_E:=(a1group1*egroup1)+ (a2group1*fgroup1)+(a3group1*ggroup1 )
ID_g2_E:=(a1group2*egroup2)+ (a2group2*fgroup2)+(a3group2*ggroup2)
total1_group1 := a4group1 + (a1group1*egroup1)+ (a2group1*fgroup1)+(a3group1*ggroup1 )
total1_group2 := a4group2 + (a1group2*egroup2)+ (a2group2*fgroup2)+(a3group2*ggroup2)
##indirect effect for hier
b1e_g1 := b1group1*egroup1 
b1e_g2 := b1group2*egroup2
b2f_g1:= b2group1*fgroup1 
b2f_g2:= b2group2*fgroup2
b3g_g1:= b3group1*ggroup1 
b3g_g2:= b3group2*ggroup2
ID_g1_H:= (b1group1*egroup1)+ (b2group1*fgroup1)+(b3group1*ggroup1 )
ID_g2_H:= (b1group2*egroup2)+ (b2group2*fgroup2)+(b3group2*ggroup2)
total1_group1 := b4group1 + (b1group1*egroup1)+ (b2group1*fgroup1)+(b3group1*ggroup1 )
total1_group2 := b4group2 + (b1group2*egroup2)+ (b2group2*fgroup2)+(b3group2*ggroup2)
##indirect effect for indi
c1e_g1 := c1group1*egroup1 
c1e_g2 := c1group2*egroup2
c2f_g1:= c2group1*fgroup1 
c2f_g2:= c2group2*fgroup2
c3g_g1:= c3group1*ggroup1 
c3g_g2:= c3group2*ggroup2
ID_g1_I:= (c1group1*egroup1)+ (c2group1*fgroup1)+(c3group1*ggroup1 )
ID_g2_I:= (c1group2*egroup2)+ (c2group2*fgroup2)+(c3group2*ggroup2)
total1_group1 := c4group1 + (c1group1*egroup1)+ (c2group1*fgroup1)+(c3group1*ggroup1 )
total1_group2 := c4group2 + (c1group2*egroup2)+ (c2group2*fgroup2)+(c3group2*ggroup2)
##indirect effect for fatal
d1e_g1 := d1group1*egroup1 
d1e_g2 := d1group2*egroup2
d2f_g1:= d2group1*fgroup1 
d2f_g2:= d2group2*fgroup2
d3g_g1:= d3group1*ggroup1 
d3g_g2:= d3group2*ggroup2
ID_g1_F:= (d1group1*egroup1)+ (d2group1*fgroup1)+(d3group1*ggroup1 )
ID_g2_F:= (d1group2*egroup2)+ (d2group2*fgroup2)+(d3group2*ggroup2)
total1_group1 := d4group1 + (d1group1*egroup1)+ (d2group1*fgroup1)+(d3group1*ggroup1 )
total1_group2 := d4group2 + (d1group2*egroup2)+ (d2group2*fgroup2)+(d3group2*ggroup2)'



avoid_model_boot_fit<-lavaan::sem(avoid_model, data=Corona_Culture,std.lv=TRUE,
                                  meanstructure=TRUE,estimator = "ML",group = "Country_ID",
                                  group.equal = "loadings",test="bootstrap",bootstrap=5000)
summary(avoid_model_boot_fit,
        fit.measure=TRUE,
        standardized=TRUE,
        rsquare=TRUE,ci=TRUE)


parameterEstimates(avoid_model_boot_fit, standardized=TRUE, se = TRUE, zstat = TRUE, pvalue = TRUE, ci = TRUE, 
                   level = 0.95, boot.ci.type = "bca.simple")

##Vaccinate
vaccinate_model<-'Egal1=~S2_CT_7+S2_CT_8+S2_CT_9
Fatal1=~S2_CT_10+S2_CT_11+S2_CT_12
Hier1=~S2_CT_1+S2_CT_2+S2_CT_3
Indi1=~S2_CT_4+S2_CT_5+S2_CT_6
RiskPerc=~S2_Risk_Personal_NoAction+S2_Risk_Country+S2_Dread
Trust1=~S2_Trust_State+S2_Trust_Local+S2_Trust_CDC
ActionPerc1=~S2_Vaccinate_Risks_low_household+S2_Vaccinate_Risks_low_community+S2_Vacinate_Resources
RiskPerc~c(a1group1, a1group2)*Egal1+c(b1group1, b1group2)*Hier1+c(c1group1, c1group2)*Indi1+c(d1group1, d1group2)*Fatal1
Trust1~c(a2group1, a2group2)*Egal1+c(b2group1, b2group2)*Hier1+c(c2group1, c2group2)*Indi1+c(d2group1, d2group2)*Fatal1
ActionPerc1~c(a3group1, a3group2)*Egal1+c(b3group1, b3group2)*Hier1+c(c3group1, c3group2)*Indi1+c(d3group1, d3group2)*Fatal1
S2_Vaccinate_Household_action~c(a4group1, a4group2)*Egal1+c(b4group1, b4group2)*Hier1+c(c4group1, c4group2)*Indi1+c(d4group1, d4group2)*Fatal1+c(egroup1, egroup2)*RiskPerc+c(fgroup1, fgroup2)*Trust1+c(ggroup1, ggroup2)*ActionPerc1
##indirect effect for egal
a1e_g1 := a1group1*egroup1 
a1e_g2 := a1group2*egroup2
a2f_g1:= a2group1*fgroup1 
a2f_g2:= a2group2*fgroup2
a3g_g1:= a3group1*ggroup1 
a3g_g2:= a3group2*ggroup2
ID_g1_E:=(a1group1*egroup1)+ (a2group1*fgroup1)+(a3group1*ggroup1 )
ID_g2_E:=(a1group2*egroup2)+ (a2group2*fgroup2)+(a3group2*ggroup2)
total1_group1 := a4group1 + (a1group1*egroup1)+ (a2group1*fgroup1)+(a3group1*ggroup1 )
total1_group2 := a4group2 + (a1group2*egroup2)+ (a2group2*fgroup2)+(a3group2*ggroup2)
##indirect effect for hier
b1e_g1 := b1group1*egroup1 
b1e_g2 := b1group2*egroup2
b2f_g1:= b2group1*fgroup1 
b2f_g2:= b2group2*fgroup2
b3g_g1:= b3group1*ggroup1 
b3g_g2:= b3group2*ggroup2
ID_g1_H:= (b1group1*egroup1)+ (b2group1*fgroup1)+(b3group1*ggroup1 )
ID_g2_H:= (b1group2*egroup2)+ (b2group2*fgroup2)+(b3group2*ggroup2)
total1_group1 := b4group1 + (b1group1*egroup1)+ (b2group1*fgroup1)+(b3group1*ggroup1 )
total1_group2 := b4group2 + (b1group2*egroup2)+ (b2group2*fgroup2)+(b3group2*ggroup2)
##indirect effect for indi
c1e_g1 := c1group1*egroup1 
c1e_g2 := c1group2*egroup2
c2f_g1:= c2group1*fgroup1 
c2f_g2:= c2group2*fgroup2
c3g_g1:= c3group1*ggroup1 
c3g_g2:= c3group2*ggroup2
ID_g1_I:= (c1group1*egroup1)+ (c2group1*fgroup1)+(c3group1*ggroup1 )
ID_g2_I:= (c1group2*egroup2)+ (c2group2*fgroup2)+(c3group2*ggroup2)
total1_group1 := c4group1 + (c1group1*egroup1)+ (c2group1*fgroup1)+(c3group1*ggroup1 )
total1_group2 := c4group2 + (c1group2*egroup2)+ (c2group2*fgroup2)+(c3group2*ggroup2)
##indirect effect for fatal
d1e_g1 := d1group1*egroup1 
d1e_g2 := d1group2*egroup2
d2f_g1:= d2group1*fgroup1 
d2f_g2:= d2group2*fgroup2
d3g_g1:= d3group1*ggroup1 
d3g_g2:= d3group2*ggroup2
ID_g1_F:= (d1group1*egroup1)+ (d2group1*fgroup1)+(d3group1*ggroup1 )
ID_g2_F:= (d1group2*egroup2)+ (d2group2*fgroup2)+(d3group2*ggroup2)
total1_group1 := d4group1 + (d1group1*egroup1)+ (d2group1*fgroup1)+(d3group1*ggroup1 )
total1_group2 := d4group2 + (d1group2*egroup2)+ (d2group2*fgroup2)+(d3group2*ggroup2)'



vaccinate_model_boot_fit<-lavaan::sem(vaccinate_model, data=Corona_Culture,std.lv=TRUE,
                                      meanstructure=TRUE,estimator = "ML",group = "Country_ID",
                                      group.equal = "loadings",test="bootstrap",bootstrap=5000)
summary(vaccinate_model_boot_fit,
        fit.measure=TRUE,
        standardized=TRUE,
        rsquare=TRUE,ci=TRUE)


parameterEstimates(vaccinate_model_boot_fit, standardized=TRUE, se = TRUE, zstat = TRUE, pvalue = TRUE, ci = TRUE, 
                   level = 0.95, boot.ci.type = "bca.simple")
